import 'package:flutter/material.dart';

typedef Clicked = Function(int,String);

enum ImageButtonStyle { topImageButtonText, leftImageRightText, leftTextRightImage }
enum AlignSide {KLeft,KCenter,KRight}

class WFImageButton extends StatefulWidget {
	final Widget image;
	final Text text;
	final Color backgroundColor;
	final EdgeInsets padding;
	final double gapBetweenImageAndText;
	final ImageButtonStyle style;
	final AlignSide alignSide;
	Clicked? onClick;
	int tag;
	String flag;
	
	WFImageButton(
	this.image,
		this.text,
			{
		this.backgroundColor = Colors.transparent,
		this.padding = const EdgeInsets.all(0),
		this.gapBetweenImageAndText=0,
		this.style = ImageButtonStyle.topImageButtonText,
		this.alignSide = AlignSide.KCenter,
		this.onClick,
		this.tag = -1,
		this.flag = "",
	});
	
	@override
	_WFImageButtonState createState() => _WFImageButtonState();
}

class _WFImageButtonState extends State<WFImageButton> {

	Widget createButton() {
		switch (widget.style) {
			case ImageButtonStyle.topImageButtonText:
				{
					return Column(
						mainAxisSize: MainAxisSize.min,
						children: <Widget>[
							widget.image,
							Padding(
								padding: EdgeInsets.only(top: widget.gapBetweenImageAndText),
								child: widget.text,
							),
						],
					);
				}
			
			case ImageButtonStyle.leftImageRightText:
				{
					return Row(
						mainAxisSize: MainAxisSize.min,
						children: <Widget>[
							widget.image,
							Padding(
								padding: EdgeInsets.only(left: widget.gapBetweenImageAndText),
								child: widget.text,
							),
						],
					);
				}
			
			case ImageButtonStyle.leftTextRightImage:
				{
					return Row(
						mainAxisSize: MainAxisSize.min,
						children: <Widget>[
							widget.text,
							Padding(
								padding: EdgeInsets.only(left: widget.gapBetweenImageAndText),
								child: widget.image,
							),
						
						],
					);
				}
		}
	}
	
	@override
	Widget build(BuildContext context) {
		return GestureDetector(
			onTap: () {
				if (widget.onClick!=null) {
					widget.onClick!(widget.tag, widget.flag);
				}
			},
			behavior: HitTestBehavior.opaque,
			child: Container(
				color: widget.backgroundColor,
			  child: Padding(
			  	padding: widget.padding,
			  	child: Row(
						mainAxisAlignment: _obtainAlign(),
			  	  children: [
			  	    createButton(),
			  	  ],
			  	),
			  ),
			),
		);
	}

	MainAxisAlignment _obtainAlign(){
		switch (widget.alignSide) {
			case AlignSide.KLeft:
				return MainAxisAlignment.start;

			case AlignSide.KCenter:
				return MainAxisAlignment.center;

			case AlignSide.KRight:
				return MainAxisAlignment.end;
		}
	}
}